//导入模型
const { Class_Target } = require('../../models/Course outline/class_Target')
const { Index_Point } = require('../../models/Course outline/index_Point')
const { class_Target_AND_Point } = require('../../models/Course outline/class_Target_AND_Point')


//课程目标名字查找
exports.Get_Class_Target_Name = async (req, res) => {
  const { t_user_and_a_c_s_id } = req.query;

  try {
    const classes = await Class_Target.findAll({
      where: {
        t_user_and_a_c_s_id: t_user_and_a_c_s_id,
      },
      attributes: ['target_name'] // 指定要返回的字段
    });

    if (classes.length === 0) {
      return res.json({
        'code': 203,
        'msg': '没有对应数据'
      });
    }

    res.json({
      'code': 200,
      'msg': '查询成功',
      'data': classes.map(classItem => classItem.target_name) // 逐个返回每个 target_name 值
    });
  } catch (error) {
    console.error('查询数据失败:', error);
    res.json({
      'code': 500,
      'msg': '查询失败'
    });
  }
};


//指标点名字查找
exports.Get_Index_Point_Name = async (req, res) => {
  const { t_user_and_a_c_s_id } = req.query;

  try {
    const classes = await Index_Point.findAll({
      where: {
        t_user_and_a_c_s_id: t_user_and_a_c_s_id,
      },
      attributes: ['point_name'] // 指定要返回的字段
    });

    if (classes.length === 0) {
      return res.json({
        'code': 203,
        'msg': '没有对应数据'
      });
    }

    res.json({
      'code': 200,
      'msg': '查询成功',
      'data': classes.map(classItem => classItem.point_name) // 逐个返回每个 target_name 值
    });
  } catch (error) {
    console.error('查询数据失败:', error);
    res.json({
      'code': 500,
      'msg': '查询失败'
    });
  }
};

//课程目标与毕业要求指标点的对应关系查询
exports.Get_Class_Target_AND_Point = async (req, res) => {
  const { t_user_and_a_c_s_id } = req.query;

  try {
    const classes = await class_Target_AND_Point.findAll({
      where: {
        t_user_and_a_c_s_id: t_user_and_a_c_s_id,
      },
    });

    if (classes.length === 0) {
      return res.json({
        'code': 203,
        'msg': '没有对应数据'
      });
    }

    res.json({
      'code': 200,
      'msg': '查询成功',
      'data': classes
    });
  } catch (error) {
    console.error('查询数据失败:', error);
    res.json({
      'code': 500,
      'msg': '查询失败'
    });
  }
};

// 添加
exports.ADD_Class_Target_And_Point = async (req, res) => {
  const { target_name, point_name, between, t_user_and_a_c_s_id } = req.body;

  try {
    // 第一步：在Class_Target表中找到与target_name和t_user_and_a_c_s_id相关的数据，并获取id
    const classTarget = await Class_Target.findOne({ where: { target_name, t_user_and_a_c_s_id } });

    if (!classTarget) {
      res.json({
        code: 404,
        msg: '未找到相关的Class_Target数据'
      });
      return;
    }

    const classTargetId = classTarget.id;

    // 第二步：在Index_Point表中找到与point_name和t_user_and_a_c_s_id相关的数据，并获取id
    let indexPoint = await Index_Point.findOne({ where: { point_name, t_user_and_a_c_s_id } });

    if (!indexPoint) {
      // 如果没有找到相关数据，则插入新数据
      indexPoint = await Index_Point.create({ point_name, t_user_and_a_c_s_id });
    }

    const indexPointId = indexPoint.id;

    // 第三步：将class_target_id、index_point_id、between和t_user_and_a_c_s_id插入到class_Target_AND_Point表中
    await class_Target_AND_Point.create({ class_target_id: classTargetId, index_point_id: indexPointId, between, t_user_and_a_c_s_id });

    res.json({
      code: 200,
      msg: '数据已插入'
    });
  } catch (error) {
    console.error('操作失败:', error);
    res.json({
      code: 500,
      msg: '操作失败'
    });
  }
};